from pandas import read_csv

from my_models import client, Datas, delete_database, Tickets


def copy_data_from_csv_to_mongo(file_path):
    """
    将csv数据导入到Mongo
    """
    print("请确保您的csv第一行为列名" + file_path)
    data = read_csv(file_path)
    for _, row in data.iterrows():
        # 此处额外进行了数据验证，MongoDB实际上可以直接插入dict数据
        # 请核对您的csv列名，否则请修改此处的row["列名"]
        blood_pressure = Datas(
            time=row["time"],
            h=row["h"],
            m=row["m"],
            sbp=row["sbp"],
            dbp=row["dbp"],
            device_id=row["device_id"],
        )
        client.blood_pressure.datas.insert_one(blood_pressure.model_dump())


def copy_ticket_from_csv_to_mongo(file_path):
    """
    将csv数据导入到Mongo
    """
    print("请确保您的csv第一行为列名" + file_path)
    data = read_csv(file_path)
    for _, row in data.iterrows():
        # 此处额外进行了数据验证，MongoDB实际上可以直接插入dict数据
        # 请核对您的csv列名，否则请修改此处的row["列名"]
        ticket = Tickets(
            device_id=row["device_id"],
            ticket=row["ticket"],
        )
        client.blood_pressure.tickets.insert_one(ticket.model_dump())


if __name__ == "__main__":
    delete_database()
    copy_data_from_csv_to_mongo("bp3.csv")
    copy_ticket_from_csv_to_mongo("tickets.csv")
